package com.zerui.manager.operationrecord.gatewayimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zerui.manager.operationrecord.OperationRecord;
import com.zerui.manager.operationrecord.convertor.OperationRecordConvert;
import com.zerui.manager.operationrecord.entity.OperationRecordDo;
import com.zerui.manager.operationrecord.gateway.OperationRecordGateWay;
import com.zerui.manager.operationrecord.mapper.OperationRecordMapper;
import com.zerui.manager.web.cmd.OperationRecordCmd;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;

/**
 * @Author wh
 * @create_time 2023-08-23 15:28
 */
@Component
public class OperationRecordGateWayImpl implements OperationRecordGateWay {

	@Resource
	private OperationRecordMapper operationRecordMapper;

	@Override
	public Page<OperationRecord> getPageList(OperationRecordCmd operationRecordCmd) {
		LambdaQueryWrapper<OperationRecordDo> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(ObjectUtils.isNotEmpty(operationRecordCmd.getDeviceId()),OperationRecordDo::getDeviceId,operationRecordCmd.getDeviceId());
		queryWrapper.orderByDesc(OperationRecordDo::getCreateTime);
		Page<OperationRecordDo> page = new Page<>(operationRecordCmd.getPageIndex(),operationRecordCmd.getPageSize());
		Page<OperationRecordDo> selectPage = operationRecordMapper.selectPage(page, queryWrapper);
		return OperationRecordConvert.INSTANCE.convertOperationRecordDoToOperationRecordPage(selectPage);
	}

	@Override
	public void insertBatch(List<OperationRecord> operationRecords) {
		List<OperationRecordDo> operationRecordDos = OperationRecordConvert.INSTANCE.convertOperationRecordsToOperationRecordDo(operationRecords);
		operationRecordMapper.insertBatchSomeColumn(operationRecordDos);
	}
}
